var Blemash = require("../BleMash-js/blemash.js");
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var Login = require("../../utils/login.js")
var app = getApp();
Page({
  data: {
    currentpage:0,
    binddistabled:false,
    saveinput:'',
    meshs_granted: []
  },
  onShow(){
    Ajax.AsyncGet('/mesh/mesh_list/', null, (data) => {
      app.globalData.meshs = [];
      app.globalData.meshsname = [];
      for (let i = 0; i < data.meshs.length; i++) {
        app.globalData.meshs.push(data.meshs[i]);
        app.globalData.meshsname.push({ name: data.meshs[i].name, meshid: data.meshs[i].id, changename: false });
      }
      this.setData({ meshs: app.globalData.meshsname });
    })
    Ajax.AsyncGet('/grant/meshs_granted_to_me/', null, (data) => {
      app.globalData.meshs_granted = data.data
      this.setData({ meshs_granted: app.globalData.meshs_granted });
    })
    this.setData({
      phoneheight: app.globalData.phoneheight,
      fix_position: app.globalData.fix_position
    });
  },
  bindchange: function (e) {
    this.setData({currentpage: e.detail.current})
  },
  checkcurrent:function(e){
    var that = this;
    if (this.data.currentpage != e.target.dataset.current){
      this.setData({currentpage: e.target.dataset.current})
    }
  },
  bindfocus:function(e){
    var index = e.currentTarget.dataset.index;
    var temp_str = "meshs[" + index + "].changename"
    this.setData({[temp_str]: true})
  },
  bindblur:function(e){
    var index = e.currentTarget.dataset.index;
    var temp_changename = "meshs[" + index + "].changename"
    var temp_name = "meshs[" + index + "].name"
    this.setData({
      [temp_changename]: false,
      [temp_name]:this.data.meshs[index].name
    })
  },
  saveinput:function (e) {
    this.setData({saveinput:e.detail.value})
  },
  changename:function(e){
    var index = e.currentTarget.dataset.index;
    Ajax.AsyncPost('/mesh/mesh_rename/', {
      meshname: this.data.saveinput,
      meshid: this.data.meshs[index].meshid
    }, (data) => {
      var temp_str = "meshs[" + index + "].name";
      this.setData({[temp_str]: this.data.saveinput});
    })
  },
  setActivatemesh:function(mesh){
    if (!app.globalData.activemesh||(mesh.id != app.globalData.activemesh.id)) {
      if (app.globalData.activemesh) app.globalData.activemesh.close();
      var blemesh = new Blemash(mesh.id, mesh.net_key, mesh.net_index, mesh.app_key, mesh.app_index, mesh.seq, mesh.iv_index, mesh.address, mesh.ttl);
      blemesh.onBleMashSeqChange(function (meshId,seq) {
        app.globalData.activemesh.seq_number = seq;
        Ajax.AsyncPost('/mesh/mesh_update_seq/', { meshid: meshId, seq: seq});
      });
      blemesh.onBleMashIvIndexChange(function (meshId, iv_index) {
        app.globalData.activemesh.iv_index = iv_index;
        Ajax.AsyncPost('/mesh/mesh_update_ivindex/', { meshid: meshId, ivindex: iv_index});
      });
      blemesh.name = mesh.name;
      blemesh.id=mesh.id;
      app.globalData.activemesh = blemesh;
    }
    wx.navigateTo({
      url: "/pages/all/all"
    })
  },
  activatemesh: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setActivatemesh(app.globalData.meshs[index]);
  },
  activate_granted_mesh(e){
    var index = e.currentTarget.dataset.index;
    this.setActivatemesh(app.globalData.meshs_granted[index]);
  },
  addmesh:function(e){
    Ajax.AsyncPost('/mesh/mesh_add/', { netkey: Blemash.getMeshNetKey(), meshname: '点击修改名字'},(data)=>{
      app.globalData.meshs.push(data.mesh);
      this.data.meshs.push({ name: data.mesh.name, meshid: data.mesh.id, changename: false })
      this.setData({ meshs: this.data.meshs})
    });
  },
  deletemesh:function(e){
    var index = e.currentTarget.dataset.index;
    var name = this.data.meshs[index].name;
    Modal.showModal("确认要删除" + name + "吗？",()=>{
      Ajax.AsyncPost('/mesh/delete_mesh/', { meshid: app.globalData.meshs[index].id}, (data) => {
        this.data.meshs.splice(index, 1);
        this.setData({meshs: this.data.meshs});
        app.globalData.meshs.splice(index, 1);
      });
    })
  }
})
